package sicnu.cs.aps.domain.dto;

import lombok.Data;
import org.springframework.beans.BeanUtils;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import sicnu.cs.aps.common.util.excel.ExcelExport;
import sicnu.cs.aps.common.util.excel.ExcelImport;
import sicnu.cs.aps.common.util.excel.ExcelLang;
import sicnu.cs.aps.domain.entity.StudentInfo;
import sicnu.cs.aps.domain.entity.User;

import java.util.Date;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 用于学生账户的导入导出
 *
 * @author chenhetao
 * @version 1.0
 * @className StudentImportDto
 * @description 用于学生账户的导入导出
 * @date 2022/5/26 15:41
 */
@Data
public class StudentImportDto {
    @ExcelImport(value = "角色类型", kv = ExcelLang.ACCOUNT_TYPE_KV)
    private int type;

    @ExcelImport("姓名")
    private String name;

    @ExcelImport(value = "学院", kv = ExcelLang.COLLAGE_TYPE_KV)
    private int collegeId;

    @ExcelImport(value = "专业", kv = ExcelLang.MAJOR_TYPE_KV)
    private int majorId;

    @ExcelImport("学号/工号")
    private String sid;

    @ExcelImport("年级")
    private String grade;

    @ExcelImport("班级")
    private String classNo;

    @ExcelImport("电话")
    private String mobile;

    @ExcelImport("邮箱")
    private String email;

    @ExcelImport(value = "性别", kv = "0- ;1-男;2-女")
    private int gender;


    /**
     * 转换当前对象为用户对象
     *
     * @return 用户对象
     */
    public User convertUser() {
        return User.builder()
                .username(String.valueOf(this.sid))
                .password("$2a$10$h8K8gtSiX4GciSeIUYR0Z.L3AxorbNjHaZILwWAwEmoQzbr5mPXBG")
                .email(this.email)
                .mobile(String.valueOf(this.mobile))
                .build();
    }

    /**
     * 转换当前对象为学生信息对象
     *
     * @return 学生信息对象
     */
    public StudentInfo convertStudentInfo() {
        int cno = 0;
        Pattern p = Pattern.compile("\\d+");
        Matcher m = p.matcher(classNo);
        m.find();
        if (m.find())
            cno = Integer.parseInt(m.group());

        return StudentInfo.builder()
                .collegeId((long) this.collegeId)
                .majorId((long) this.majorId)
                .name(this.name)
                .classNo(cno)
                .grade(Integer.parseInt(grade.substring(0, 4)))
                .gender(this.gender)
                .build();
    }

}
